import os
import sys
import site

# mypyutils.virtualenv must already be imported as virtualenv when calliong this script via execfile

# My own version of activate_this.py here, since the official one is broken under windows
base = virtualenv.locate_virtualenv()
if base is None:
    print 'Error: Could not locate virtualenv!'
    sys.exit(-1)
site_packages = os.path.join(base, virtualenv.SITE_PACKAGES)
prev_sys_path = list(sys.path)
site.addsitedir(site_packages)
sys.real_prefix = sys.prefix
sys.prefix = base
# Move the added items to the front of the path:
new_sys_path = []
for item in list(sys.path):
    if item not in prev_sys_path:
        new_sys_path.append(item)
        sys.path.remove(item)
sys.path[:0] = new_sys_path
